
<?php
wp_enqueue_script('jquery-ui-datepicker');
//wp_enqueue_style('jquery-style', 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.2/themes/smoothness/jquery-ui.css');

?> 
<link rel="stylesheet" type="text/css" href="<?php echo plugins_url(); ?>/parish_data/css/jquery-ui.css">
<style type="text/css">
.form_control { width:99%; min-height:35px; border:1px solid #ccc; padding:0px 10px; border-radius:4px;}
.btnFull { min-height:38px; width:96%; margin:0 2%; line-height:36px;}
</style>

<script type="text/javascript">
	/* <![CDATA[ */
	jQuery(document).ready(function(){
		jQuery('#memberlist').dataTable();
	});
	/* ]]> */
	
	jQuery(document).ready(function() {
    jQuery('#to_date').datepicker({
        dateFormat : 'dd-mm-yy'
    });
	 jQuery('#from_date').datepicker({
        dateFormat : 'dd-mm-yy'
    });
});

</script>
<?php
	
	require_once("userclass.php");
	$objUser = new userClass();
	
	require_once("paymentclass.php");
	$objPayment = new paymentClass();
	
	require_once("trackclass.php");
	$objTrack = new trackClass();
	
	global $wpdb;
	
	
	
	
	
	
	
	/*$query_rep = mysql_query("SELECT * FROM parishioner WHERE religious_study!=0");
		while ($row = mysql_fetch_array($query_rep))
			{
			echo $row['id'].'-'.$row['name'].'-'.$row['religious_study'];
			echo'<br>';
			
			mysql_query("INSERT INTO  `wordpress`.`catchism_students` (
`id` ,
`parishioner_id` ,
`class` ,
`status`
)
VALUES (
NULL ,  '".$row['id']."',  '".$row['religious_study']."',  '0'
);
");
			}*/
	
	if($_REQUEST['report'] == 'yes'){
	
	
	
		echo '<script>
						 window.open("'.get_site_url().'/XLS/catechism.csv","_blank"); 
						 window.location.href = "'.get_site_url().'/wp-admin/admin.php?page=reports"; 
					</script>';
					
	}
	
?>



<div class="wrap">
    <h2>Sales Report &nbsp;&nbsp;</h2>
    <form action="" method="post">
  
              <table class="" width="100%" border="0" cellspacing="0" cellpadding="0"  bgcolor="#666666" style="color:#FFF">
  <tbody>
    <tr>
      <td width="20%"  style="padding-left:5px;">Unit</td>
      <td width="20%">Class</td>
      <td width="20%">Order Status </td>
      <td width="24%"><table width="100%" border=" 0" cellspacing="5" cellpadding="0">
          <tbody>
            <tr>
              <td>Default(Order Date)</td>
               
            </tr>
          </tbody>
      </table></td>
      <td width="10%">&nbsp;</td>
      <td width="10%">&nbsp;</td>
    </tr>
    <tr>
      <td style="padding-left:5px;"><select name="unit" class="form_control" ><option value="">Select</option>
                					 <option <?php echo (($_POST['payment_status']== 1) ? 'selected="selected"' : '');?> value="1">St Antony</option>		
                					 <option <?php echo (($_POST['payment_status']== 2) ? 'selected="selected"' : '');?> value="2">St Peter</option>	
                                     <option <?php echo (($_POST['payment_status']== 3) ? 'selected="selected"' : '');?> value="3">St james</option>	
                                     <option <?php echo (($_POST['payment_status']== 4) ? 'selected="selected"' : '');?> value="4">Mariyam Thresia</option>	
									 <option <?php echo (($_POST['payment_status']== 5) ? 'selected="selected"' : '');?> value="5">St Thomas</option>	
									  <option <?php echo (($_POST['payment_status']== 6) ? 'selected="selected"' : '');?> value="6">Bl Evuprasiama</option
									 ><option <?php echo (($_POST['payment_status']== 7) ? 'selected="selected"' : '');?> value="7">St Sebastian</option>	 						                                     <option <?php echo (($_POST['payment_status']== 8) ? 'selected="selected"' : '');?> value="8">St Alphonsa</option>	 
									 <option <?php echo (($_POST['payment_status']== 9) ? 'selected="selected"' : '');?> value="9">St Joseph</option>	 
										 
								
                                                      
                        			</select></td>
      <td><select name="class" class="form_control" >
                            <option value="">class</option>
                         
						   <option value="1">1</option>
						   <option value="2">2</option>
						   <option value="3">3</option>
						   <option value="4">4</option>
						   <option value="5">5</option>
						   <option value="6">6</option>
						   <option value="7">7</option>
						   <option value="8">8</option>
						   <option value="9">9</option>
						   <option value="10">10</option>
						   <option value="11">11</option>
						   <option value="12">12</option>
                           </select></td>
      <td><select name="track_status" class="form_control" >3
                            <option value="">Select Status</option>
                            <?php echo $objTrack->getTrackSelect($_POST['track_status']); ?>
                           </select></td>
      <td><table width="100%" border="0" cellspacing="5" cellpadding="0">
        <tbody>
          <tr>
            <td><input type="text" id="from_date" name="from_date" value="<?php echo $_POST['from_date']; ?>" placeholder="From Date" class="form_control" >
				  </td>
            <td><input type="text" id="to_date" name="to_date" value="<?php echo $_POST['to_date']; ?>" placeholder="To Date" class="form_control" ></td>
          </tr>
        </tbody>
      </table></td>
      <td><a href="admin.php?page=reports&report=yes&user=<?php echo $id; ?>"></a> <input type="submit" value="Search" class="button button-primary btnFull" name="serch_val" />    </td>
      <td style="padding-right:10px;"><u class="btnFull" style="">
	  
	  <?php 	if($_POST['serch_val']){ ?>
	  <a style="color:#FFF; margin-top:15px; text-decoration:none ;float:right;" href="<?php echo get_site_url().'/XLS/catechism.csv';?>" >Print</a>
	  
	  <?php } ?>
	  
	  <!--href="admin.php?page=reports&report=yes&user=<?php echo $id; ?>" -->
	
	  </u></td>
    </tr>
  </tbody>
</table>
           </form>
		   
		   <form name="update_promotionlist" method="post" action="">
	 <table width="100%" class="wp-list-table widefat fixed " id="memberlist">
		<thead>
			<tr><th><u>Ser. No.</u></th>
				<th width="50"><u>Order<br> ID</u></th>
					<th><u>Reg Number</u></th>
				<th><u>Student Name</u></th>
			
				<th width="70"><u>Date Of Birth</u></th>
				<th><u>Class</u></th>
            
				<th><u>Half Yearly <br /> Theory</u></th>
				    <th><u>Half Yearly<br /> Internal</u></th>
						<th><u>Annual<br />Theory </u></th>
				<th><u>Annual <br />Internal</u></th>
			
				<th>Attendance</th>
				<th>Totlal</th>
				<th>Pass Status</th>
				<th>View Details</th>
			</tr>
		</thead>
		<tbody>
		<script type="text/javascript">
				/* <![CDATA[ */
				jQuery(document).ready(function(){
					jQuery('#mytable').dataTable();
				});
				/* ]]> */

			</script>
<?php

		if(isset($_POST['save_student_details'])){
			$reg_no = array();
			$reg_no = $_POST['reg_no'];
			foreach($reg_no as $key=>$val){
				mysql_query('UPDATE  `catchism_students` SET  `reg_no` =  '.$val.' WHERE  `catchism_students`.`parishioner_id` ='.$key.';');
			}
			
			$half_internal = array();
			$half_internal = $_POST['half_internal'] ? $_POST['half_internal'] : '';
			
			$half_theory = array();
			$half_theory = $_POST['half_theory'] ? $_POST['half_theory'] : '';
			
			$annual_internal = array();
			$annual_internal = $_POST['annual_internal'] ? $_POST['annual_internal'] : '';
			
			$annual_theory = array();
			$annual_theory = $_POST['annual_theory'] ? $_POST['annual_theory'] : '';
			
			$attendance = array();
			$attendance = $_POST['attendance'] ? $_POST['attendance'] : '';
			
			foreach($half_internal as $key=>$val){
			
				$cont = mysql_query("SELECT COUNT(id) AS count FROM catechism_promotion_list WHERE parishioner_id = ".$key.";");
				$row  = mysql_fetch_array($cont);
				if($row['count']==0)
						mysql_query("INSERT INTO `catechism_promotion_list` (`id`, `parishioner_id`, `half_internal`) VALUES (NULL, '".$key."', '".$val."');");
				else{
			
						mysql_query('UPDATE  `catechism_promotion_list` SET  `half_internal` =  '.$val.',
																			 `half_theory`   =  '.$half_theory[$key].',
																			 `annual_internal` = '.$annual_internal[$key].',
																			 `annual_theory` = '.$annual_theory[$key].',
																			 `attendance` = '.$attendance[$key].'
																			  WHERE  `catechism_promotion_list`.`parishioner_id` ='.$key.';');	
						}	
				#mysql_query('UPDATE  `catchism_students` SET  `reg_no` =  '.$val.' WHERE  `catchism_students`.`parishioner_id` ='.$key.';');
				#mysql_query("INSERT INTO `catechism_promotion_list` (`id`, `parishioner_id`, `half_internal`) VALUES (NULL, '".$key."', '".$val."');");
			}
			
			
		}






		if($_REQUEST['user']!='')
			$addon1 = ' AND   wp_spec_assigned_orders.assigned_to ='.$_REQUEST['user'].'';
		 if($_REQUEST['payment_status']!='')	
		    $addon2 = ' AND  wp_spec_checkout.payment_status='.$_REQUEST['payment_status'].'';
		 if($_REQUEST['track_status']!=0){
			   if($_POST['track_status']==8)
						$addon3= ' AND wp_spec_order_trac.track_status != 7 AND wp_spec_order_trac.track_status != 6';
				else		
						$addon3 = ' AND wp_spec_order_trac.track_status='.$_REQUEST['track_status'].'';	
			}
		 if(($_REQUEST['from_date']!='') && ($_REQUEST['to_date']!='')){
			
			
			# FROM DATE
			$from_date		 = $_REQUEST['from_date'];
			$from_date_array = explode("-", $from_date);
			$date  =  $from_date_array[0]; 
			$month =  $from_date_array[1]; 
			$year  =  $from_date_array[2]; 
			$fromdate = mktime(0,0,0,$month,$date,$year);
			
			
			# TO DATE
			$to_date		 = $_REQUEST['to_date'];
			$to_date_array = explode("-", $to_date);
			$date  =  $to_date_array[0]; 
			$month =  $to_date_array[1]; 
			$year  =  $to_date_array[2]; 
			$todate = mktime(0,0,0,$month,$date,$year);		
		
	
				$addon4 = 'AND wp_spec_checkout.payment_time  BETWEEN '.$fromdate.' AND '.$todate.'';
			}
			
			if($_REQUEST['unit']!='')	
				$addon1 = ' AND   parish_ward.ward_no ='.$_REQUEST['unit'].'';	
			 
			if($_REQUEST['class']!='')	
				$addon2 = ' AND   catchism_students.class ='.$_REQUEST['class'].'';		
									
	        $sql = "SELECT 
						catchism_students.class,catchism_students.reg_no,
						parishioner.id, parishioner.name, parishioner.date_of_birth ,
						catechism_promotion_list.half_internal,
						catechism_promotion_list.half_theory,
						catechism_promotion_list.annual_internal,
						catechism_promotion_list.annual_theory,
						catechism_promotion_list.attendance
						
						FROM catchism_students
						INNER JOIN parishioner ON catchism_students.parishioner_id = parishioner.id
						LEFT JOIN catechism_promotion_list ON catchism_students.parishioner_id = catechism_promotion_list.parishioner_id
						WHERE catchism_students.pass_out = 0
						
						$addon2 GROUP BY catchism_students.parishioner_id";
						
					
		$result = mysql_query($sql) or die ('Error, query failed');
		if (mysql_num_rows($result) > 0 )
		{
		?>
				
<?php		$marks_array = array();
			$content[]= array(); 
			$content[]= array('','',' ','','OUR LADY OF GRACE CHURCH','','','','');
			$content[]= array('','',' ','','Promotion List 2014-15','','','','');
			$content[]= array('Serial No','Reg.Number','Student Name','Date Of Birth','HY.Theory','HF. Internal','FL.Theory','FL.Internal','Total Marks','Attandance','Pass Status');
			$i = 1;
			while ($row = mysql_fetch_assoc($result))
			{
				$total           =0;
				$id		 		 = $row['id'];
				$name     		 = $row['name'];
				$date_of_birth	 = $row['date_of_birth'];
				$unit_name	     = $row['ward_name'];
				$class		     = $row['class'];	
							
				$reg_no  		 = $row['reg_no'];
				
				
				$half_internal   = $row['half_internal'];
				$half_theory     = $row['half_theory'];
				$annual_internal = $row['annual_internal'];
				$annual_theory   = $row['annual_theory'];
				
				$total = $half_internal+$half_theory+$annual_internal+$annual_theory;
				$marks_array = $total;
				if($total>35){
					$pass_status = 'Passed';
					}
				else{
					$pass_status = 'Failed';	
					}
					
				$attendance   = $row['attendance'];
				
						
				//if($row['payment_time']>0)
				//$time				 =  date("M j, Y", $row['payment_time']);			
				
			$content[]= array($i,$reg_no, $name,$date_of_birth,$half_theory,$half_internal,$annual_theory,$annual_internal,$total,$attendance,$pass_status);
				//$content[]= array('#'.$order_id);
	?>
			<tr>
			    <td><?php echo $i;?></td>
				<td><?php echo $id; ?></td>
				<td nowrap><input type="text" size="40" placeholder="H Internal" style="width:80px;" id="title" name="reg_no[<?php echo $row['id'];?>]" value="<?php echo $reg_no;?>"></td>
				<td nowrap><?php echo $name; ?></td>
				<td nowrap><?php echo $date_of_birth; ?></td>
				<td nowrap><?php echo $class; ?></td>
				
				<td nowrap><input type="text" size="40" placeholder="H Theory" style="width:80px;" id="title" name="half_theory[<?php echo $row['id'];?>]"
				 value="<?php echo $half_theory;?>"></td>
				 <td nowrap><input type="text" size="40" placeholder="H Internal" style="width:80px;" id="title" name="half_internal[<?php echo $row['id'];?>]" 
				value="<?php echo $half_internal;?>"></td>
			
				<td nowrap><input type="text" size="40" placeholder="A Theory" style="width:80px;" id="title" name="annual_theory[<?php echo $row['id'];?>]" 
				value="<?php echo $annual_theory;?>"></td>
					<td nowrap><input type="text" size="40" placeholder="A Internal" style="width:80px;" id="title" name="annual_internal[<?php echo $row['id'];?>]" 
				value="<?php echo $annual_internal;?>"></td>
				<td nowrap><input type="text" size="40" placeholder="Attendance" style="width:80px;" id="title" name="attendance[<?php echo $row['id'];?>]" 
				value="<?php echo $attendance;?>"></td>
			
                
				<td nowrap><?php echo $total;?></td>
					<td nowrap><?php echo $pass_status;?></td>
				
				<td width="10%" nowrap><u style="background-color:  #0074A2; text-align:center;color:#FFF; border: 2px solid; min-width:50px; display:block;
    border-radius: 50px;text-decoration:none;" ><a style="color:#FFF; text-decoration:none" target="_blank" href="admin.php?page=order_more_info&act=upd&id=<?php echo $id; ?>">View Details</a></u>
</td>
			</tr>
			
<?php $i = $i+1;	

	 }
	 $content[]= array('','',' ','','','','','','');
			$content[]= array('','',' ','','','','','','');
	 	$content[]= array('','','Class First','','','','');
			$content[]= array('','',' ','','','','','','');
			$content[]= array('','',' ','','','','','','');
				$content[]= array('','','Class Second','','','','');
				$content[]= array('','',' ','','','','','','');
					$content[]= array('','',' ','','','','','','');
					$content[]= array('','','Prents Prize','','','','');
					$content[]= array('','',' ','','','','','','');
					$content[]= array('','',' ','','','','','','');
						$content[]= array('','','Full Attandance','','','','');
		
	 	
		if($_POST['serch_val']){
			$file_name = 'catechism';
			$path = ''.get_home_path().'XLS/'.$file_name.'.csv';
			if(file_exists($path)) {
			unlink($path);
			}
			$list = $content;
			$fp = fopen($path, 'w');
			foreach ($list as $fields) {
				fputcsv($fp, $fields);
			}
			fclose($fp);
		}	
	} else { ?>
			<tr>
				<td>No Record Found!</td>
				<td nowrap></td>
				<td nowrap></td>
				<td nowrap></td>
                <td nowrap></td>
				<td nowrap></td>
				<td nowrap></td>
				<td nowrap></td>
				<td nowrap></td>
				<td nowrap></td>
				<td nowrap></td>
				<td nowrap></td>
				<td width="10%" nowrap>
</td>
			</tr>
			
	<?php } ?>
	
	
	</tbody>
	</table>
	
	<table>
	<tr><td > <input type="submit" value="Save Data" name="save_student_details" /></td><td nowrap></td>
				<td nowrap></td>
				<td nowrap></td><td nowrap></td>
                <td nowrap></td>
				<td nowrap></td>
				<td nowrap></td>
				<td nowrap></td>
				<td nowrap></td>
				<td nowrap></td>
				<td nowrap></td>
				<td nowrap></td></tr>
				</table>
	</form>
	
</div>
<script>
jQuery( "#toplevel_page_admin-page-reports" ).removeClass();
jQuery( "#toplevel_page_admin-page-reports" ).addClass( "wp-has-submenu wp-has-current-submenu wp-menu-open menu-top menu-icon-generic toplevel_page_myplug/muyplg" );
jQuery("#toplevel_page_admin-page-reports").css("background-color", "#0074a2");
</script>	